package com.wgy.day43Json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.util.*;

public class JacksonTest {
    @Test
    public void test1() throws Exception {
        //1.创建Person对象
        Person p=new Person();
        p.setName("张三");
        p.setAge(20);
        p.setGender("男");
        p.setBirthday(new Date());
        System.out.println(p);//Person{name='张三', age=20, gender='男', birthday=Wed Jan 13 15:59:03 CST 2021}

        //2.创建Jackson的核心对象 objectMapper
        ObjectMapper mapper=new ObjectMapper();
        //3.转换java对象为Jason对象
        String json = mapper.writeValueAsString(p);
        System.out.println(json);//{"name":"张三","age":20,"gender":"男","birthday":1610524789017}

        //转换java对象为Jason对象.并将数据写到d://小云/abc/jason.text文件中
       // mapper.writeValue(new File("d://小云/abc/jason.text"),p);//{"name":"张三","age":20,"gender":"男","birthday":1610524238695}

       //转换java对象为Jason对象.并将数据关联到字符输入流中
      mapper.writeValue(new FileWriter("d://小云/abc/jason.text"),p);//{"name":"张三","age":20,"gender":"男","birthday":1610524435555}

    }
    @Test
    public void test2() throws Exception {
        //1.创建Person对象
        Person p=new Person();
        p.setName("张三");
        p.setAge(20);
        p.setGender("男");
        p.setBirthday(new Date());

        Person p1=new Person();
        p1.setName("张三");
        p1.setAge(20);
        p1.setGender("男");
        p1.setBirthday(new Date());

        Person p2=new Person();
        p2.setName("张三");
        p2.setAge(20);
        p2.setGender("男");
        p2.setBirthday(new Date());

        List<Person> list=new ArrayList<Person>();
        list.add(p);
        list.add(p1);
        list.add(p2);

        //2.创建Jackson的核心对象 objectMapper
        ObjectMapper mapper=new ObjectMapper();
        String json = mapper.writeValueAsString(list);
        System.out.println(json);//[{"name":"张三","age":20,"gender":"男","birthday":"2021-01-13"},{"name":"张三","age":20,"gender":"男","birthday":"2021-01-13"},{"name":"张三","age":20,"gender":"男","birthday":"2021-01-13"}]

    }

    @Test
    public void test3() throws Exception {
        //1.创建Map对象
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("name","张三");
        map.put("age",23);
        map.put("gender","男");

        //2.创建Jackson的核心对象 objectMapper
        ObjectMapper mapper=new ObjectMapper();
        String json = mapper.writeValueAsString(map);
        System.out.println(json);//{"gender":"男","name":"张三","age":23}
    }


    @Test
        //将json对象转换成java对象
    public void test4() throws Exception {
        //1.初始化json对象字符串
           String json="{\"name\":\"张三\",\"age\":20,\"gender\":\"男\"}";

        //2.创建Jackson的核心对象 objectMapper
        ObjectMapper mapper=new ObjectMapper();
        Person person = mapper.readValue(json, Person.class);
        System.out.println(person);//Person{name='张三', age=20, gender='男', birthday=null}


    }
}
